package com.example.a80205050.myapplication.Dao;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

import com.example.a80205050.myapplication.Modle.Tb_flag;

/**
 * Created by 80205050 on 2017/10/30.
 */
public class FlagDao {
    private DBOpenHelper helper;
    private SQLiteDatabase db;
    public FlagDao(Context context) {
        helper=new DBOpenHelper(context);
    }
    /**
     * 添加便签的信息
     * @param tb_flag
     */
    public void add(Tb_flag tb_flag){
        db=helper.getWritableDatabase();
        db.execSQL("insert into tb_flag (_id, flag) values(?,?)",
                new Object[]{tb_flag.get_id(),tb_flag.getFlag()});
    }
    /**
     * 更新便签的信息
     * @param tb_flag
     */
    public void updata(Tb_flag tb_flag){
        db=helper.getWritableDatabase();
        db.execSQL("update tb_flag set flag=? where _id=?",new Object[]{
                tb_flag.getFlag(),tb_flag.get_id()
        });
    }
    /**
     * 查找便签的信息
     * @param id
     * @return
     */
    public Tb_flag find(int id){
        db=helper.getWritableDatabase();
        Cursor cursor=db.rawQuery("select _id flag from tb_flag where _id=?",new String[]{String.valueOf(id)});
        if(cursor.moveToNext()){
            return new Tb_flag(cursor.getInt(cursor.getColumnIndex("_id")),
                    cursor.getString(cursor.getColumnIndex("flag")));
        }
        return null;
    }

    /**
     * 删除便签的信息
     * @param ids
     */
    public void delete(Integer... ids){
        if(ids.length>0){
            StringBuilder sb=new StringBuilder();
            for(int i=0;i<ids.length;i++){
                sb.append('?').append(',');
            }
            sb.deleteCharAt(sb.length()-1);
            db=helper.getWritableDatabase();
            db.execSQL("delete from tb_flag where _id in ("+sb+")",(Object[])ids);
        }
    }

    /**
     * 获取便签的信息
     * @param start 开始的位置
     * @param count  每页的显示数量
     * @return
     */
    public List<Tb_flag> getScrollData(int start,int count){
        List<Tb_flag> listTb_flags=new ArrayList<Tb_flag>();
        db=helper.getWritableDatabase();
        Cursor cursor=db.rawQuery("select * from tb_flag limit ?,?",new String[]{String.valueOf(start),String.valueOf(count)});
        while (cursor.moveToNext()){
            listTb_flags.add(new Tb_flag(cursor.getInt(cursor.getColumnIndex("_id")),
                    cursor.getString(cursor.getColumnIndex("flag"))));
        }
        return listTb_flags;
    }
    /**
     * 返回总的记录数
     * @return
     */
    public long getCount(){
        db=helper.getWritableDatabase();
        Cursor cursor=db.rawQuery("select count(_id) from tb_flag",null);
        if(cursor.moveToNext()){
            return cursor.getLong(0);
        }
        return 0;
    }
    /**
     * 获取便签的最大编号
     * @return
     */
    public int getMaxId(){
        db=helper.getWritableDatabase();
        Cursor cursor=db.rawQuery("select max(_id) from tb_flag",null);
        while (cursor.moveToLast()){
            return cursor.getInt(0);
        }
        return 0;//默认返回0
    }
}
